import { login, logout, getInfo } from "@/api/login";
import { apiDictList } from "@/api/sysadmin";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
    state: {
        token: getToken(),
        name: "",
        avatar: "",
        sysDict: new Map(),
        currentRole: "",
        //mainMenu: [],
        mainMenu: [],
        myuser: null
    },

    mutations: {
        SYS_DICT_INIT: (stat, list) => {
            for (var i = 0, len = list.length; i < len; i++) {
                var dest = null;
                if (!stat.sysDict.has(list[i].dictKey)) {
                    dest = new Map();
                    stat.sysDict.set(list[i].dictKey, dest);
                } else {
                    dest = stat.sysDict.get(list[i].dictKey);
                }
                dest.set(list[i].dictValue, list[i].dictLabel);
            }
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_NAME: (state, name) => {
            state.name = name;
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar;
        },
        SET_DICT_MAP: (state, dictMap) => {
            state.dictMap = dictMap;
        },
        SET_USER: (state, myuser) => {
            state.myuser = myuser;
        },
        SET_MAIN_MENU: (state, menu) => {
            console.log("mainMenu: ", menu);
            console.log("state.mainMenu 1: ", state.mainMenu);
            state.mainMenu = menu.concat();
            console.log("state.mainMenu 2: ", state.mainMenu);
        }
    },

    actions: {
        // 登录
        Login({ commit }, userInfo) {
            const username = userInfo.username.trim();
            return new Promise((resolve, reject) => {
                login(username, userInfo.password)
                    .then(response => {
                        const data = response.data;
                        const tokenStr = data.tokenHead + data.token;
                        setToken(tokenStr);
                        commit("SET_TOKEN", tokenStr);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },

        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo()
                    .then(response => {
                        const data = response.data;
                        if (data.user) {
                            commit("SET_USER", data.user);
                            commit("SET_MAIN_MENU", data.menu);
                            commit("SET_AVATAR", data.avatar);
                        } else {
                            reject("getInfo: user must be a non-null object !");
                        }
                        resolve(response);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },

        InitDictMap({ commit, state }) {
            return new Promise((resolve, reject) => {
                apiDictList()
                    .then(response => {
                        const data = response.data;
                        //console.log("response.data %o", response.data);
                        commit("SYS_DICT_INIT", data);
                        resolve(response);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },

        // 登出 { commit }, userInfo
        //LogOut({ commit, state }) {

        // 前端 登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit("SET_TOKEN", "");
                commit("SET_USER", null);
                commit("SET_AVATAR", "");
                removeToken();
                resolve();
            });
        },

        SetAvatar({ commit }, avatar) {
            commit("SET_AVATAR", avatar);
        }
    }
};

export default user;
